#include "viewmodel.h"
#include "actions.h"
#include <QDebug>
#include <QApplication>
#include <QtConcurrentMap>
#include "globaldefinition.h"
#include "myprogramm.h"
#include "clientgui.h"
#include "dcpp/Client.h"
#include "dcpp/SettingsManager.h"
#include "dcpp/ShareManager.h"

const QString defUserEmail = "";

using namespace dcpp;
using namespace std;

ViewModel::ViewModel(MyProgramm *parent)
    : QObject(parent),
      m_page(Pages::media),
      m_prog(parent),
      m_hubs(QList<QObject*>())
{
    connect(Actions::mediaPage(), SIGNAL(triggered()), SLOT(goToMediaPage()));
    connect(Actions::searchPage(), SIGNAL(triggered()), SLOT(goToSearchPage()));
    connect(Actions::uploadsPage(), SIGNAL(triggered()), SLOT(goToUploadsPage()));
    connect(Actions::chatPage(), SIGNAL(triggered()), SLOT(goToChatPage()));
    connect(Actions::hubsPage(), SIGNAL(triggered()), SLOT(goToHubsPage()));
    connect(Actions::exit(), SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
}

const QList<QObject *> &ViewModel::hubs() const{
    return m_hubs;
}

void ViewModel::addedNewHub(Client *client){
    qDebug()<<Q_FUNC_INFO<<_q(client->getAddress());
    m_hubs<<new ClientGUI(client, this);
    emit hubsChange();
    if(m_hubs.size() == 1)
        emit mainHubChange();
}

Pages::PagesEnum ViewModel::page() const{
    try{
        return m_page;
    }catch(...){
        return Pages::media;
    }
}

QString ViewModel::pageName() const{
    try{
        return Pages::m().valueToKey(page());
    }catch(...){
        return "media";
    }
}

QString ViewModel::mediaURL() const{
    try{
        return m_mediaURL;
    }catch(...){
        return "";
    }
}

QString ViewModel::registrationURL() const{
    try{
        return m_registrationURL;
    }catch(...){
        return "";
    }
}

QString ViewModel::helpURL() const{
    try{
        return m_helpURL;
    }catch(...){
        return "";
    }
}

int ViewModel::connectionType() const{
    try{
        return SETTING(INCOMING_CONNECTIONS);
    }catch(...){
        qDebug()<<trUtf8("Ошибка")<<Q_FUNC_INFO<<__LINE__;
        return SettingsManager::getInstance()->get(SettingsManager::INCOMING_CONNECTIONS , true);
    }
}

void ViewModel::setConnectionType(int connectionType){
    try{
        if(SETTING(INCOMING_CONNECTIONS) != connectionType){
            SettingsManager::getInstance()->set(SettingsManager::INCOMING_CONNECTIONS, connectionType);
            emit connectionTypeChange();
        }
    }catch(...){
        qDebug()<<trUtf8("Ошибка")<<Q_FUNC_INFO<<__LINE__;
    }
}

QString ViewModel::searchStr() const{
    try{
        return m_searchStr;
    }catch(...){
        return "";
    }
}

int ViewModel::searchFileSize() const{
    try{
        return m_searchFileSize;
    }catch(...){
        return 0;
    }
}

QString ViewModel::searchFileType() const{
    try{
        return m_searchFileType;
    }catch(...){
        return "";
    }
}

QList<VMSearchResult *> ViewModel::searchResult() const{
    try{
        return m_searchResult;
    }catch(...){
        return QList<VMSearchResult *>();
    }
}

QList<QObject *> ViewModel::uploadsList() const{
    try{
        return m_uploadsList;
    }catch(...){
        return QList<QObject *>();
    }
}

QList<QObject *> ViewModel::downloudsList() const{
    try{
        return m_downloudsList;
    }catch(...){
        return QList<QObject *>();
    }
}

QList<QObject *> ViewModel::downloudedList() const{
    try{
        return m_downloudedList;
    }catch(...){
        return QList<QObject *>();
    }
}

const QString &ViewModel::userEmail() const{
    try{
        return m_userEmail;
    }catch(...){
        STANDART_ERROR_MASSAGE;
        return defUserEmail;
    }
}

void ViewModel::setUserEmail(const QString &userEmail)
{
    try{
        if(userEmail != this->userEmail()){
            m_userEmail = userEmail;
            emit userEmailChange();
        }
    }catch(...){
        STANDART_ERROR_MASSAGE
    }
}

SharedDirList & ViewModel::sharedDirs(){
    try{
        QtConcurrent::map(m_sharedDirList_buf, [](SharedDir * obj) { delete obj;});
        m_sharedDirList_buf.clear();
        foreach (auto var, ShareManager::getInstance()->getDirectories()) {
            m_sharedDirList_buf<<new SharedDir(var.first, var.second);
            return m_sharedDirList_buf;
        }
    }catch(...){
        STANDART_ERROR_MASSAGE;
        return m_sharedDirList_buf;
    }
}

void ViewModel::setPage(const Pages::PagesEnum page){
    if(m_page != page){
        m_page = page;
        emit pageChange();
    }
}

void ViewModel::setMediaURL(const QString & mediaURL){
    if(m_mediaURL != mediaURL){
        m_mediaURL = mediaURL;
        emit mediaURLChange();
    }
}

void ViewModel::setRegistrationURL(const QString &registrationURL){
    if(m_registrationURL != registrationURL){
        m_registrationURL = registrationURL;
        emit registrationURLChange();
    }
}

void ViewModel::setHelpURL(const QString &helpURL){
    if(m_helpURL != helpURL){
        m_helpURL = helpURL;
        emit helpURLChange();
    }
}

void ViewModel::setSearchStr(const QString &searchStr){
    if(m_searchStr != searchStr){
        m_searchStr = searchStr;
        emit searchStrChange();
    }
}

void ViewModel::setSearchFileSize(const int searchFileSize){
    if(m_searchFileSize != searchFileSize){
        m_searchFileSize = searchFileSize;
        emit searchFileSizeChange();
    }
}

void ViewModel::setSearchFileType(const QString &searchFileType){
    if(m_searchFileType != searchFileType){
        m_searchFileType = searchFileType;
        emit searchFileTypeChange();
    }
}

QAction *ViewModel::mediaPage() const{
    return Actions::mediaPage();
}

QAction * ViewModel::hubsPage() const{
    return Actions::hubsPage();
}

QAction * ViewModel::chatPage() const{
    return Actions::chatPage();
}

QAction *ViewModel::uploadsPage() const{
    return Actions::uploadsPage();
}

QAction *ViewModel::searchPage() const{
    return Actions::searchPage();
}

QAction *ViewModel::exit() const{
    return Actions::exit();
}

QAction *ViewModel::minimize() const{
    return Actions::minimize();
}

QAction *ViewModel::settings() const{
    return Actions::settings();
}

QObject *ViewModel::mainHub() const{
    //    qDebug()<<Q_FUNC_INFO<<hubs().size();
    if(hubs().size() > 0)
        return hubs()[0];
    else
        0;
}

void ViewModel::goToMediaPage(){
    setPage(Pages::media);
}

void ViewModel::goToSearchPage(){
    setPage(Pages::search);
}

void ViewModel::goToUploadsPage(){
    setPage(Pages::uploads);
}

void ViewModel::goToChatPage(){
    setPage(Pages::chat);
}

void ViewModel::goToHubsPage(){
    setPage(Pages::hubs);
}
